﻿using System;
using System.Globalization;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;

namespace Salient.Web.Administration.AppConfig
{
    public partial class DebugAndTrace : ApplicationConfigurationPage
    {
        public void BackToPreviousPage(object sender, EventArgs e)
        {
            ReturnToPreviousPage(sender, e);
        }

        private void Page_Load()
        {
            if (!IsPostBack)
            {
                System.Configuration.Configuration config = OpenWebConfiguration(ApplicationPath);
                var compilationSection = (CompilationSection) config.GetSection("system.web/compilation");
                EnableDebugCheckBox.Checked = compilationSection.Debug;

                var traceSection = (TraceSection) config.GetSection("system.web/trace");
                DisplayTraceInfoCheckBox.Checked = traceSection.PageOutput;
                DisplayTraceOutputList.SelectedIndex = (traceSection.LocalOnly) ? 0 : 1;

                EnableTraceCheckBox.Checked = traceSection.Enabled;
                SortTraceList.SelectedIndex = (traceSection.TraceMode == TraceDisplayMode.SortByCategory) ? 1 : 0;

                MostRecentTracesList.SelectedIndex = (traceSection.MostRecent) ? 0 : 1;

                int requestLimit = traceSection.RequestLimit;
                switch (traceSection.RequestLimit)
                {
                    case 5:
                        RequestLimitList.SelectedIndex = 0;
                        break;
                    case 15:
                        RequestLimitList.SelectedIndex = 2;
                        break;
                    case 20:
                        RequestLimitList.SelectedIndex = 3;
                        break;
                    case 25:
                        RequestLimitList.SelectedIndex = 4;
                        break;
                    case 30:
                        RequestLimitList.SelectedIndex = 5;
                        break;
                    case 35:
                        RequestLimitList.SelectedIndex = 6;
                        break;
                    case 40:
                        RequestLimitList.SelectedIndex = 7;
                        break;
                    case 45:
                        RequestLimitList.SelectedIndex = 8;
                        break;
                    case 50:
                        RequestLimitList.SelectedIndex = 9;
                        break;
                    default:
                        RequestLimitList.SelectedIndex = 1;
                        break;
                }

                if (!traceSection.Enabled)
                {
                    ToggleTraceSettingsElements(false);
                }
            }
        }

        public void ToggleTraceSettingsElements(bool enabled)
        {
            DisplayTraceInfoCheckBox.Enabled = enabled;
            DisplayTraceOutputList.Enabled = enabled;
            SortTraceList.Enabled = enabled;
            RequestLimitList.Enabled = enabled;
            MostRecentTracesList.Enabled = enabled;
        }

        public void WebControl_ValueChanged(object sender, EventArgs e)
        {
            var control = (Control) sender;
            System.Configuration.Configuration config = OpenWebConfiguration(ApplicationPath);
            var traceSection = (TraceSection) config.GetSection("system.web/trace");

            switch (control.ID)
            {
                case "EnableDebugCheckBox":
                    var compilationSection = (CompilationSection) config.GetSection("system.web/compilation");
                    compilationSection.Debug = EnableDebugCheckBox.Checked;
                    break;
                case "DisplayTraceInfoCheckBox":
                    traceSection.PageOutput = DisplayTraceInfoCheckBox.Checked;
                    break;
                case "DisplayTraceOutputList":
                    traceSection.LocalOnly = (DisplayTraceOutputList.SelectedIndex == 0);
                    break;
                case "EnableTraceCheckBox":
                    traceSection.Enabled = EnableTraceCheckBox.Checked;
                    ToggleTraceSettingsElements(traceSection.Enabled);
                    break;
                case "SortTraceList":
                    traceSection.TraceMode = (SortTraceList.SelectedIndex == 0)
                                                 ? TraceDisplayMode.SortByTime
                                                 : TraceDisplayMode.SortByCategory;
                    break;
                case "RequestLimitList":
                    traceSection.RequestLimit = Convert.ToInt32(RequestLimitList.SelectedItem.Value,
                                                                CultureInfo.InvariantCulture);
                    break;
                case "MostRecentTracesList":
                    traceSection.MostRecent = (MostRecentTracesList.SelectedIndex == 0) ? true : false;
                    break;
                    // TODO: Remove the exception as this is to capture development error where a control event has not been handled.
                    //       Note that it does not need to be localized.
                default:
                    throw new HttpException((string) GetLocalResourceObject("InvalidId") + control.ID);
            }

            SaveConfig(config);
        }
    }
}